package com.acme.bankapp.domain.account;

import com.acme.bankapp.exception.NotEnoughFundsException;
import com.acme.bankapp.exception.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {

    public double overdraft;

    public CheckingAccount(double startBalance, double overdraft) {
	super(startBalance);
	if (overdraft < 0) {
	    throw new IllegalArgumentException();
	}
	this.overdraft = overdraft;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (!super.equals(obj)) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	CheckingAccount other = (CheckingAccount) obj;
	if (overdraft != other.overdraft) {
	    return false;
	}
	return true;
    }

    /**
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
	int result = getClass().hashCode() + super.hashCode() + (int)overdraft;
	return result;
    }

    @Override
    public double maximumAmountToWithdraw() {
	return super.maximumAmountToWithdraw() + overdraft;
    }

    @Override
    public void withdraw(double x) throws NotEnoughFundsException {
	if (maximumAmountToWithdraw() <= x) {
	    throw new OverDraftLimitExceededException(
		    "Over draft limit exceeded.", maximumAmountToWithdraw());
	}
	super.withdraw(x);
	if (balance < 0) {
	    overdraft += balance;
	    balance = 0;
	}
    }
}
